Oa.controller('mainControl',function($scope,$http,$rootScope,$interval){
	
	$scope.getUserInfo = function(id){
		$http.get(getUserById_url,{
			params:{
				userId:id
			}
		})
		.success(function(res){
			if (res.code==200) {
			//	$scope.user = res.data;
				$scope.newuser = res.data;
				$rootScope.user = res.data;
				var year = parseInt(res.data.birth.substr(0,4));
				var date = new Date();
				var dateyear = date.getFullYear();
				$rootScope.user.age = dateyear - year;
			}
		})
		.error(function(){

		})
	}
	$scope.getUserInfo(sessionStorage.userid);
//	$scope.newuser = $rootScope.user;

	$scope.updateUserById = function(){
		$scope.newuser.birth = $("#id-date-picker-1").val();
		$scope.newuser.hiredate = $("#id-date-picker-2").val();
		console.log($("#id-date-picker-2").val());
		$scope.newuser.password = null;
//		console.log($scope.newuser);
		$http.post(updateUserById_url,$scope.newuser)
		.success(function(res){
			if (res.code==200) {
				Notify(res.msg,"top-right");
				$rootScope.user = res.data;
				$scope.newuser = res.data;
				sessionStorage.email = res.data.email;
				
				$scope.getUserInfo(sessionStorage.userid);
//				$scope.getUserInfo(sessionStorage.userid);
			}else {
				Notify(res.data,"top-right");
			}
		})
		.error(function(){
			Notify("客户端发送错误，请稍后重试！","top-right");
		})
	}


	$("#askout").on('click', function () {
        bootbox.myforms({
            message: $("#outApplication").html(),
            title: "外出申请",
            className: "modal-darkorange",
            show:true,
            buttons: {
                success: {
                    label: "发送",
                    className: "btn-primary",
                    type:"submit",
                    callback: function (result) {
											var outaddress = result.sheng+result.shi+result.qu;
						
											var obj = {
												sender:sessionStorage.email,
												name:sessionStorage.username,
												receiver:result.receiver,
												type:3,
												dateRange:result.daterange,
												info:result.info,
												outAddress:outaddress,
												user:sessionStorage.userid
											}
											
											$scope.addApply(obj);
											// bootbox.hideAll();
                    }
                },
            }
        });

    });

	$("#askwork").on('click', function () {
        bootbox.myforms({
            message: $("#workApplication").html(),
            title: "加班单",
            className: "modal-darkorange",
            show:true,
            buttons: {
                success: {
                    label: "发送",
                    className: "btn-primary",
                    type:"submit",
                    callback: function (result) {
                    	// bootbox.hideAll();
											var obj = {
												sender:sessionStorage.email,
												name:sessionStorage.username,
												receiver:result.receiver,
												type:2,
												dateRange:result.daterange,
												info:result.info,
												outAddress:null,
												user:sessionStorage.userid
											}
										//	console.log(result);
											$scope.addApply(obj);
                    }
                },
            }
        });

    });

	$("#askleave").on('click', function () {
        bootbox.myforms({
            message: $("#LeaveApplication").html(),
            title: "请假单",
            className: "modal-darkorange",
            show:true,
            buttons: {
                success: {
                    label: "发送",
                    className: "btn-primary",
                    type:"submit",
                    callback: function (result) {
											// bootbox.hideAll();
                    						
                    		
											var obj = {
												sender:sessionStorage.email,
												name:sessionStorage.username,
												receiver:result.receiver,
												type:1,
												dateRange:result.daterange,
												info:result.info,
												outAddress:null,
												user:sessionStorage.userid
											}
										//	console.log(result);
											$scope.addApply(obj);
											
                    }
                },
            }
        });

    });

	
	$("#leave-select2").change(function(){
		var xly = $(this).val();
		console.log(xly);
	});
	//我要请假,出差，加班
	$scope.addApply = function(apply){
		$http.post(addApply_url,apply)
		.success(function(res){
			if (res.code==200) {
				Notify(res.msg,"top-right");
			}else {
				Notify(res.msg,"top-right");
			}
		})
		.error(function(){
			Notify("客户端错误","top-right");
		})
	}


	//获取审批回复
	$scope.getReply = function(){
		$http.get(getMyApply_url,{
      params:{
        sender:sessionStorage.email
      }
    })
    .success(function(res){
      if (res.code==200) {
				$scope.reply = [];
				for (var i = 0; i < res.data.length; i++) {
					if (res.data[i].dealStatus && !res.data[i].reades) {
						var a = new Date(res.data[i].sendDate);
	          res.data[i].sendDate = a.toLocaleString();
						$scope.reply.push(res.data[i]);
					}
				}
				$scope.replyNum = $scope.reply.length;
      }else {
        Notify(res.data,"top-right");
      }
    })
    .error(function(){
      Notify("获取失败，请稍后重试","top-right");
    });
	}
	$scope.getReply();
	$interval($scope.getReply,100000000);
	
	$http.get(getAllUser_url,
		{
			params:{
				page:1,
				size:10000
			}
		})
			.success(function(res){
				if (res.code==200) {
					$scope.usersapply = res.data.list;
				}
			}).error(function(){
				Notify('客户端错误',"top-right");
	});


});
